package algorithm;

public class CharReverse {
    public static void main(String[] args) {
        char[] ch = {'h', 'e', 'l', 'l', 'o'};
        //reverse(ch);
        System.out.println(reverse2(ch,0,ch.length-1));
    }

    // 原地反转
    public static void reverse(char[] ch) {
        for (int i = 0; i < ch.length/2; i++) {
            char temp = ch[i];
            ch[i] = ch[ch.length - 1 - i];
            ch[ch.length - 1 - i] = temp;
        }
    }

    // 递归双指针
    public static String reverse2(char[] ch, int left, int right) {
        if (left >= right) {
            return new String(ch);
        }
        char temp = ch[left];
        ch[left] = ch[right];
        ch[right] = temp;
        return reverse2(ch, ++left, --right);
    }
}
